var _api = require("../../utils/api"), _cardUtil = _interopRequireDefault(require("../../utils/card-util")), _util = _interopRequireDefault(require("../../public/utils/util")), _user = _interopRequireDefault(require("../../public/utils/user")), _behaviorCreate = _interopRequireDefault(require("../behavior-create"));

function _interopRequireDefault(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

var LIMIT = 20;

Component({
    behaviors: [ _behaviorCreate.default ],
    properties: {
        card: {
            type: Object,
            value: {},
            observer: function(i) {
                var r = this;
                clearTimeout(this.timer), this.timer = wx.setTimeout(function() {
                    var t = (i = i || {
                        loading: !0
                    }) ? i.cardid : "", e = i ? i.guid : "";
                    r.$setData({
                        guideShown: !t,
                        show: !t,
                        loading: i.loading || !1
                    }), t ? e || r.selectCorpShow(i) : r.init();
                }, 500);
            }
        }
    },
    attached: function() {
        this.offset = 0, this.lists = [];
    },
    ready: function() {
        var i = this;
        _user.default.getTokens(function(t) {
            var e = function(t) {
                var e = t.accesstoken;
                return i.setData({
                    accesstoken: e
                });
            };
            t ? e(t) : _user.default.listenToken(e);
        });
    },
    data: {
        loading: !0,
        groupedList: {},
        groupedKeys: [],
        guideShown: !1
    },
    methods: {
        init: function() {
            this.firstLoad || (this.firstLoad = !0, this.setServiceCard(), this.loadHistory(!0));
        },
        setServiceCard: function() {
            var e = this;
            _api.json.serviceList().then(function(t) {
                e.setData({
                    serviceList: t.lists
                });
            });
        },
        onPullDownRefresh: function(t) {
            this.loadHistory(!0, t);
        },
        onReachBottom: function() {
            var t = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : function() {};
            if (this.nomore) return t();
            this.loadHistory(!1, t);
        },
        loadHistory: function(e, t) {
            var i = this;
            e && (this.offset = 0), _api.api.weappcard.history({
                data: {
                    limit: LIMIT,
                    offset: this.offset,
                    isqyy: 0,
                    sortby: "addtime",
                    orderby: "desc"
                },
                complete: t
            }).then(function(t) {
                return i.handleList(t, e);
            });
        },
        handleList: function(t, e) {
            var i = t.data;
            e && (this.lists = []), i.forEach(function(t) {
                _cardUtil.default.formatCardData(t);
            });
            var r = this.lists.concat(i);
            this.lists = r, this.setData(Object.assign({
                loading: !1,
                count: r.length
            }, _util.default.groupByTimestampDesc(r, "updatetime"))), this.offset += LIMIT, 
            this.nomore = i.length < LIMIT;
        },
        goView: function(t) {
            var e = t.currentTarget.dataset, i = e.cardid, r = e.guid;
            this.$route("/pages/card/view/view?cardid=".concat(i, "&guid=").concat(r));
        },
        tapMyCard: function() {
            this.triggerEvent("mycard");
        }
    }
});